﻿

//------------------------------------------------------------------------
//架构还不错，逻辑通过适配器渲染,这部分渲染依赖逻辑，另外渲染要去更新（轮训）逻辑
//即避免了互相耦合，也做到了逻辑和渲染分离的目的。
//------------------------------------------------------------------------

#include "CGEEvent.h"
#include "GameGraphics.h"
#include "ChessGame.h"
#include "SDL_gfxPrimitives.h"
#include "CGETimer.h"
#include <time.h>
#include <iostream>
//shenhe:发现内存泄露，当AI走到棋子上，却不移动时（这本身就是一个A bug），再每走一步就会再次分配内存
//shenhe:MFC 的使用:在共享 DLL 中使用 MFC，其他的机器就可以运行。但是若选择使用标准 Windows 库，则其他机器会显示 应用程序 配置不正确之类的。
//shenhe:发现一个BUG，选中棋子后，如果不断点击要移动的位置，棋子会沿着这个方向一直前进，而不无法停下。没想到这是movedistance未初始化造成的，但是在调试模式下就不会发生，直接运行exe就会。
//shenhe:用SDL image载入tga图片时，在PC平台是可以正常显示的。但是iphone上就无法看到，断点看到的信息是内存空间已分配，但是surface里的很多项值是0。难道是文件名问题，要测试一下
//shenhe:两个bug,(1)AI被进球之后，不等球放入原始位置就开始移动（2）有时候AI没移到球上，但是球会莫民奇妙的移动
//shenhe:目前的头文件包含形式是要用到的类，直接在头文件中include。这样做并不好。A include B,C include A。若B重新编译，则A，C都会重新编译。因为目前项目小，编译时间本就不长。
//更好的方式是用什么类，在头文件中声明，然后在源文件中包含。这样可避免不必要的重新编译。前面例子，C就不需要重新编译。
GameGraphics* g_Render = NULL;
ChessGame *g_chessGame = NULL;
CGETimer *g_Timer = NULL;



int main( int argc, char* args[] )
{
	srand( (unsigned)time( NULL ) );

	//Quit flag
	bool quit = false;

	SDL_Init( SDL_INIT_EVERYTHING );
	CGESpriteMgr->SetScreenSurface(SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE /* | SDL_FULLSCREEN */));
	
	CGEEvent* inputEvent = new CGEEvent();
	g_Render = new GameGraphics();
	g_chessGame = new ChessGame();
	g_Timer = new CGETimer();

	g_chessGame->Initialize();
	g_Render->AttachToGame(g_chessGame);


	SDL_Surface* screen = CGESpriteMgr->GetScreenSurface();

	int mouseX = 0,mouseY = 0;
	float boardX = 0.0f,boardY = 0.0f;
	//While the user hasn't quit
	while( quit == false )
	{
		g_Timer->Start();
		//If there's an event to handle
		while( inputEvent->PollEvent() )
		{
			//If a mouse button was released
			if( inputEvent->GetEventType() == CGE_MOUSEBUTTONUP )
			{
				//If the left mouse button was released
				if( inputEvent->GetMouseButton() == CGEMB_LEFT )
				{ 
					inputEvent->GetMousePos(&mouseX,&mouseY);
					g_Render->Get2DIntersection(mouseX,mouseY,boardX,boardY);
					g_chessGame->OnSelection(boardX, boardY);
					g_chessGame->CheckBallRealease(mouseX,mouseY);
					g_chessGame->CheckDoubleClick(DoubleClickDetected());
					std::cout <<"up"<<std::endl;
				}
			}
			if(inputEvent->GetEventType() == CGE_MOUSEBUTTONDOWN)
			{
				//If the left mouse button was released
				if( inputEvent->GetMouseButton() == CGEMB_LEFT )
				{ 
					std::cout <<"down"<<std::endl;
					inputEvent->GetMousePos(&mouseX,&mouseY);
					g_chessGame->CheckBallPressed(mouseX,mouseY);
				}
			}
			if( inputEvent->GetEventType() == CGE_KEYUP)
			{
				SDL_SaveBMP(CGESpriteMgr->GetScreenSurface(),"test1.bmp");
				std::cout <<"save bmp"<<std::endl;
				//no use,only PC
			}
			//If the user has Xed out the window
			if( inputEvent->GetEventType() == CGE_QUIT  )
			{
				//Quit the program
				quit = true;
			}
		}

		//Fill the screen white
		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) ); 

		g_Render->Prepare( 1.0f / g_FramePerSecond,inputEvent);
		g_Render->Render();


		LockFrame(g_Timer);

	}
	CGESpriteMgr->DelAll();
	delete g_Render;
	g_Render = NULL;
	delete g_chessGame;
	g_Render = NULL;
	SDL_Quit();
	return 0;
}